package com.dinglian.ots.controller.buyer;

import com.dinglian.ots.exception.GlobalException;
import com.dinglian.ots.result.CodeMsg;
import com.dinglian.ots.result.Result;
import com.dinglian.ots.service.CartService;
import com.dinglian.ots.vo.cart.CartListVo;
import com.dinglian.ots.vo.cart.CartVo;
import com.dinglian.ots.vo.cart.DeleteCartVo;
import com.dinglian.ots.vo.cart.UpdateCartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangbo
 */
@Slf4j
@RestController
@Api(value = "/cart", tags = "购物车模块")
@RequestMapping("/cart/")
@CrossOrigin
public class CartController {

    @Autowired
    private CartService cartService;

    @Value("${pic.url}")
    private String url;

    @ApiOperation("添加到购物车")
    @PostMapping("addCart")
    public Result addCart(@RequestBody CartVo cartVo) {
        try {
            cartService.addCart(cartVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @ApiOperation(value = "修改购物车", notes = "sign 符号 1-加号 2-减号")
    @PostMapping("updateCart")
    public Result updateCart(@RequestBody UpdateCartVo cartVo) {
        Map amt = new HashMap();
        try {
            amt =  cartService.updateCart(cartVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(amt);
    }

    @ApiOperation("查询购物车列表")
    @GetMapping("getCartList")
    public Result getCartList(@RequestParam(value = "userId", required = true) Integer userId) {
        CartListVo cartListVo = new CartListVo();
        try {
            cartListVo = cartService.getCartList(userId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successWithUrl(cartListVo, url);
    }

    @ApiOperation("删除购物车中的产品")
    @PostMapping("deleteCart")
    public Result deleteCart(@RequestBody DeleteCartVo cartVo) {
        try {
            cartService.deleteCart(cartVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @ApiOperation("查询购物车政策")
    @GetMapping("getCartListRule")
    public Result getCartListRule(@RequestParam(value = "userId", required = true) Integer userId,
                                  @RequestParam(value = "brandId", required = true) Integer brandId) {
        Map ruleMap = new HashMap();
        try {
            ruleMap = cartService.getCartListRule(userId,brandId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successWithUrl(ruleMap, url);
    }



}
